package com.kedll.library.util;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.kedll.library.KBaseApplication;
import com.kedll.library.base.Constants;
import com.kedll.library.location.entity.LocationEntity;

/**
 * 配置相关管理工具
 *
 * @author slj
 * @date 2014/9/19
 */
public class SharePreUtils {

    private final static String GLOBAL_PREFERENCE = "global_preference";
    protected static Context context = KBaseApplication.getInstance();

    /**
     * 添加登录的用户
     *
     * @param ctx
     * @param user
     */
    public static void addUser(Context ctx, String user) {
        JSONArray array1 = new JSONArray();
        JSONArray array = getUsers(ctx);
        if (array.contains(user)) {
            array.remove(user);
        }

        array1.add(user);
        array1.addAll(array);

        StoreShareValue.putString("users", array1.toJSONString(), ctx, StoreShareValue.USER);
    }

    /**
     * 获取已经登录过的用户列表
     *
     * @param ctx
     * @return
     */
    public static JSONArray getUsers(Context ctx) {
        String users = StoreShareValue.getString("users", "[]", ctx, StoreShareValue.USER);
        return JSON.parseArray(users);
    }

    /**
     * 用户账户
     *
     * @param ctx
     * @param account
     */
    public static void saveAccount(Context ctx, String account) {
        StoreShareValue.putString("account", account, ctx, StoreShareValue.USER_LOGIN_INFO);
    }

    public static String getAccount(Context ctx) {
        return StoreShareValue.getString("account", "", ctx, StoreShareValue.USER_LOGIN_INFO);
    }

    public static void saveObject(String key, Object value) {
        StoreShareValue.putObject(key, value, context, "OBJECT_DATA");
    }

    public static Object getObject(String key) {
        return StoreShareValue.getObject(key, "OBJECT_DATA", context);
    }

    public static void deleteObject(String key) {
        StoreShareValue.removeKey(key, context, "OBJECT_DATA");
    }

    public static void saveAppConfig(Object value) {
        StoreShareValue.putObject("appConfig", value, context, "APP_CONFIG");
    }

    public static Object getAppConfig() {
        return StoreShareValue.getObject("appConfig", "APP_CONFIG", context);
    }

    public static void savaOssConfig(String value) {
        StoreShareValue.putObject("ossConfig", value, context, "OSS_CONFIG");
    }

    public static Object getOssConfig() {
        return StoreShareValue.getObject("ossConfig", "OSS_CONFIG", context);
    }

    /**
     * @param version
     */
    public static void setLastVersionCode(int version) {
        StoreShareValue.putInt("version", version, context, StoreShareValue.SAVE_KEY_TOKEN);
    }


    /**
     * 是否是新的引导版本
     *
     * @return
     */
    public static boolean isNewNavigateCode() {
        return Constants.NAVIGATE_CODE > getLastNavigateCode();
    }

    /**
     * 当前的引导版本号
     *
     * @return
     */
    public static int getLastNavigateCode() {
        return StoreShareValue.getInt("last_navigate_code", 0, context, StoreShareValue.SAVE_KEY_TOKEN);
    }

    /**
     * 设置新的引导版本号
     */
    public static void setNewNavigateCode() {
        StoreShareValue.putInt("last_navigate_code", Constants.NAVIGATE_CODE, context, StoreShareValue.SAVE_KEY_TOKEN);
    }

    /**
     * 存储定位信息
     *
     * @param ctx
     * @param entity
     */
    public static void saveLocation(Context ctx, LocationEntity entity) {
        StoreShareValue.putString("location", entity.toString(), ctx, StoreShareValue.USER);
    }

    public static LocationEntity getLocation(Context ctx) {
        String entity = StoreShareValue.getString("location", "", ctx, StoreShareValue.USER);
        return TextUtils.isEmpty(entity) ? null : JSON.parseObject(entity, LocationEntity.class);
    }
}
